package com.htfyun.fragment.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.htfyun.utils.DebugLog;
import com.htfyun.view.EmptyRecyclerView;

import org.geometerplus.zlibrary.ui.android.BuildConfig;
import org.geometerplus.zlibrary.ui.android.R;

import java.util.ArrayList;
import java.util.List;

import me.yokeyword.fragmentation.SupportFragment;

/**
 * Created by sst on 2017/12/29.
 */

public abstract class BaseWithPageNumberFragment<T> extends SupportFragment {

    private View layoutRecyclerView;
    private EmptyRecyclerView mRecy;
    private TextView txtEmpty;
    private FrameLayout LayoutEmptyView;

    private ImageView imgNext;
    private ImageView imgPrevious;
    private TextView txtPageNumberTotalItem;
    private TextView txtPageNumberPercent;

    private View layoutPageNumber;

    private int currentPage;

    private CheckBox wifiEnableCheckBox;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.htfyun_base_page_number_fragment, container, false);

        if (!BuildConfig.USE_PHONE) {
            view.setEinkUpdateMode((short)3, (short)3);
        }

        initView(view);
        return view;
    }

    private void initView(View view) {

        layoutRecyclerView = view.findViewById(R.id.layoutRecyclerView);

        layoutPageNumber = view.findViewById(R.id.layoutPageNumber);

        wifiEnableCheckBox = (CheckBox) view.findViewById(R.id.wifiEnableCheckBox);

        mRecy = (EmptyRecyclerView) view.findViewById(R.id.recyclerView);
        txtEmpty = (TextView) view.findViewById(R.id.emptyView);
        txtEmpty.setText(getStringForTextEmpty());

        LayoutEmptyView = (FrameLayout) view.findViewById(R.id.LayoutEmptyView);
        mRecy.setEmptyView(LayoutEmptyView);
        mRecy.setNestedScrollingEnabled(false);

        imgNext = (ImageView) view.findViewById(R.id.imgNext);
        imgPrevious = (ImageView) view.findViewById(R.id.imgPrevious);
        txtPageNumberTotalItem = (TextView) view.findViewById(R.id.txtPageNumberTotalItem);
        txtPageNumberPercent = (TextView) view.findViewById(R.id.txtPageNumberPercent);

        imgNext.setOnClickListener(onClickListener);
        imgPrevious.setOnClickListener(onClickListener);

        mRecy.setLayoutManager(getLayoutManager());

        BaseWithPageNumberAdapter adapter = getAdapter();
        if (adapter != null) {
            adapter.setOnAdapterBindViewHolderListener(new BaseWithPageNumberAdapter.OnAdapterBindViewHolderListener() {
                @Override
                public void onBindStart() {

                    DebugLog.e("lockVRITraversal");
                    layoutRecyclerView.lockVRITraversal(500);
                }

                @Override
                public void onBindEnd() {

                    DebugLog.e("unlockVRITraversal");
                    layoutRecyclerView.unlockVRITraversal();

                }
            });
            mRecy.setAdapter(adapter);

        }

        if (getDecor() != null) {
            mRecy.addItemDecoration(getDecor());
        }

        currentPage = 1;

    }

    public CheckBox getWifiEnableCheckBox() {
        return wifiEnableCheckBox;
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        configUI();
    }

    public void configUI() {

        configTxtPageNumberTotalItem();
        configTxtPageNumberPercent();

    }

    public void rebuildUI() {

        configUI();

    }

    @SuppressWarnings("unchecked")
    private void configAdapter() {
        List<T> totalItems = getTotalItemList();
        List<T> showItems = new ArrayList<>();

        for (int index = (currentPage - 1) * getItemCountPerPage(), i = 0;
             i < getItemCountPerPage() && index < totalItems.size(); index++, i++) {

            showItems.add(totalItems.get(index));
        }

        RecyclerView.Adapter adapter = mRecy.getAdapter();
        if (adapter instanceof BaseWithPageNumberAdapter) {
            ((BaseWithPageNumberAdapter) adapter).setItemsAndNotify(showItems);
        }

    }

    private void configTxtPageNumberPercent() {

        txtPageNumberPercent.setText(getString(R.string.base_page_number_percent, currentPage, getTotalPage()));
        txtPageNumberPercent.setVisibility(View.VISIBLE);


        int totalPage = getTotalPage();
        if (totalPage == 0 || totalPage == 1) {

            imgPrevious.setVisibility(View.INVISIBLE);
            imgNext.setVisibility(View.INVISIBLE);
            txtPageNumberPercent.setVisibility(View.INVISIBLE);

        } else if (currentPage == 1) {
            imgPrevious.setVisibility(View.INVISIBLE);
            imgNext.setVisibility(View.VISIBLE);

        } else if (currentPage == totalPage) {
            imgPrevious.setVisibility(View.VISIBLE);
            imgNext.setVisibility(View.INVISIBLE);
        } else {
            imgPrevious.setVisibility(View.VISIBLE);
            imgNext.setVisibility(View.VISIBLE);
        }
        configAdapter();

    }

    private void configTxtPageNumberTotalItem() {
        int totalItem = 0;
        if (getTotalItemList() != null) {
            totalItem = getTotalItemList().size();
        }

        txtPageNumberTotalItem.setText(getString(R.string.base_page_number_total, totalItem));
        txtPageNumberTotalItem.setVisibility(View.VISIBLE);

    }

    private int getTotalPage() {
        int totalItem = 0;
        int itemCountPerPage = 1;
        if (getTotalItemList() != null) {
            totalItem = getTotalItemList().size();
        }

        if (getItemCountPerPage() > 0) {
            itemCountPerPage = getItemCountPerPage();
        }

        int totalPage = totalItem / itemCountPerPage;
        if (totalItem % itemCountPerPage != 0) {
            totalPage++;
        }

        return totalPage;

    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            boolean changed = false;
            if (v == imgNext) {
                if (currentPage < getTotalPage()) {
                    currentPage++;
                    changed = true;
                }

            } else if (v == imgPrevious) {
                if (currentPage > 1) {
                    currentPage--;
                    changed = true;
                }

            }
            if (changed) {
                configTxtPageNumberPercent();
            }

        }
    };

    public void setEmptyView(View view) {
//        mRecy.setEmptyView(view);
        LayoutEmptyView.removeAllViews();
        LayoutEmptyView.addView(view);
    }

    public void setLayoutPageNumberVisibility(int visibility) {
        layoutPageNumber.setVisibility(visibility);
    }

    public abstract RecyclerView.LayoutManager getLayoutManager();

    public abstract RecyclerView.ItemDecoration getDecor();

    public abstract BaseWithPageNumberAdapter getAdapter();

    public abstract int getItemCountPerPage();

    public abstract List<T> getTotalItemList();

    public abstract String getStringForTextEmpty();



}
